#include "CaptureFilter.h"

CaptureFilter::CaptureFilter(void)
{
	HRESULT hr;

	hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**) &g_pCapture);

	if(SUCCEEDED(hr))
	{
		hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**) &g_pGraph);

		if(SUCCEEDED(hr))
		{
			g_pCapture->SetFiltergraph(g_pGraph);
		}
	}

	hr = FindCaptureDevice();
}

HRESULT CaptureFilter::FindCaptureDevice(void)
{
	HRESULT hr;

	ICreateDevEnum *pDevEnum = NULL;
	IEnumMoniker *pClassEnum = NULL;

	IMoniker *pMoniker = NULL;
	IBaseFilter *pSrc = NULL;


	 // Create the system device enumerator
    hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &pDevEnum);

	hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pClassEnum, 0);

	hr = pClassEnum->Next(1, &pMoniker, NULL);

	hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc);

	hr = g_pGraph->AddFilter(pSrc, L"Video Capture");

	return hr;
}